import request from "@/utils/request";

// 获取文章列表
export const getPosts = (params?: {
  page?: number;
  limit?: number;
  userId?: string;
}) => {
  return request.get<IResponse<IPostListResponse>>("/posts", { params });
};

// 根据ID获取单篇文章
export const getPostById = (id: string) => {
  return request.get<IResponse<{ post: IPost }>>(`/posts/${id}`);
};

// 创建文章
export const createPost = (data: { title: string; content: string }) => {
  return request.post<IResponse<{ post: IPost }>>("/posts", data);
};

// 更新文章
export const updatePost = (
  id: string,
  data: {
    title: string;
    content: string;
  }
) => {
  return request.put<IResponse<{ post: IPost }>>(`/posts/${id}`, data);
};

// 删除文章
export const deletePost = (id: string) => {
  return request.delete<IResponse<null>>(`/posts/${id}`);
};

// 搜索文章
export const searchPosts = (params: {
  q: string;
  page?: number;
  limit?: number;
}) => {
  return request.get<IResponse<IPostSearchResponse>>("/posts/search", {
    params,
  });
};
